// ************************* 78 character ruler *****************************
//
// Title:       BIT_CheckOpticalLoop.c
// Purpose:     A short description of the implementation.
//
// Created on:  2013-3-1 at 15:00:35 by Vinson.Zhang
// Copyright:   dafda. All Rights Reserved.
//

// ************************* 78 character ruler *****************************

#include "RFBB.h"         
#include <utility.h>
#include <formatio.h> 
#include "USB6501.h"
                  

_DAQMX_WRITE_PORT_CMD g_szSFPWritePort0Cmd[2] = {{"SFP_ON","DIO1/port0",LMPCARDON},      
												 {"SFP_OFF","DIO1/port0",LMPCARDOOFF}};    

                                                 
_FRMON_TEST_STRUCT_CMD g_cFRMONOpticalLoopCmd[6] = {{"FRMon,action,load,agent,MURKKU2Access,Vegas,_MURKKU2Access","done"},
                                                        {"FRMon/Vegas/_MURKKU2Access,action,init,MURKKU2Access,","done"},
                                                        {"FRMon/Vegas/_MURKKU2Access,action,opticalLoopTest,1","0x00"},
														{"FRMon/Vegas/_MURKKU2Access,action,opticalLoopTest,2","0x00"},
														{"FRMon/Vegas/_MURKKU2Access,action,opticalLoopTest,3","0x00"},
                                                        {"FRMon/Vegas/_MURKKU2Access,action,terminate,MURKKU2Access","done"}};

static char g_szShmonOpticalLoopInital[BUFFER_SIZE_64] = "muksush -w 321c 3ffff";


//==============================================================================
// Global functions

/**************************************************************************************************
  Function:         BIT_CheckOpticalLoop
  Description:      This test is to check the I2C interface between MURKKU and SFP connectors.
  Calls:            none
  Called By:        dll
  Table Accessed:   none
  Table Updated:    none
  Input:            none
  Output:           error code
  Return:           none
  Test Case ID:     FRHD_CheckOpticalLoop
  Used Command:     g_szCmonSfpLosCmd
**************************************************************************************************/
void __decldllexp TX_TEST BIT_CheckOpticalLoop(tTestData *testData, tTestError *testError)
{
    int error = 0;
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;

    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = 0;
    char  kszReturnString[BUF_SIZE_32K+1] = {'\0'};
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};
    char szElementPath[BUFFER_SIZE_256] ={'\0'};
    double dTestNum = 0.0;

    int iPassFail= 0; 
    int iSfpNum = 0;
    int iArrayOffset = 0;
	long int lReturnVal = 0;
    int iReturn = 0;
    int i = 0;
    int iArryNum = 0;
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                           TS_SeqContextStep, CAVT_OBJHANDLE, 
                                           &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                    &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                           TS_UIMsg_ProgressText, 0.0,
                                           pszStepName, 0, VTRUE));

    //--------------------------------------------------------------------
    // Get Test Parameters from Sequence FileGlobals
    //--------------------------------------------------------------------
    sprintf(szElementPath, "%s.%s.PARAMETERS.TestNum", TESTPLAN_BASE_PATH, pszStepName );   
    tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szElementPath, 0, &dTestNum));

    if((dTestNum >= 1) && (dTestNum <= 3))
    	{
        i = dTestNum;
    	}
    else
		{
		goto Error;
		}
	
   
	if(dTestNum == 1)
    	{
		 //InetTelnetOpen(FRCommand)	
		g_hFrmonAgent = RfbbInetTelnetOpen (g_szDUTIP, g_iPort, 0);          

    	for(iArryNum = 0;iArryNum < 2;iArryNum++)
    		{
        	sprintf(kszShmonAgent,g_cFRMONOpticalLoopCmd[iArryNum].cWriteFrmonCmd);    
      		FRmonAgent_String(kszShmonAgent, -1,g_cFRMONOpticalLoopCmd[iArryNum].cReturnValue, &iPassFail);
	
        	if (iPassFail!=1) 
        		{
				 //action terminate
    			sprintf(kszShmonAgent,g_cFRMONOpticalLoopCmd[5].cWriteFrmonCmd);
    			Frmon_RunCmdNoAck(kszShmonAgent, 1); 
    		
				//InetTelnetClose(FRCommand)    
				InetTelnetClose(g_hFrmonAgent);
            	goto Error;
        		}
			}
		}
		sprintf(kszShmonAgent,g_cFRMONOpticalLoopCmd[i + 1].cWriteFrmonCmd);    
      	FRmonAgent_String(kszShmonAgent, -1,g_cFRMONOpticalLoopCmd[i + 1].cReturnValue, &iPassFail);
		Delay(1);
    	if(dTestNum == 3) 
		{
			 	//action terminate
    			sprintf(kszShmonAgent,g_cFRMONOpticalLoopCmd[5].cWriteFrmonCmd);
    			Frmon_RunCmdNoAck(kszShmonAgent, 1); 
    			//InetTelnetClose(FRCommand)
				InetTelnetClose(g_hFrmonAgent);
            	goto Error;
		}
    
Error: 
    
    if( iPassFail == PASS) 
    	{
        testData->result = PASS;
    	}
    else 
    	{
        testData->result = FAIL;
    	}

    
    // FREE RESOURCES
	
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    	{
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    	}

    // Clear message from GUI
    TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

    return;    
}

/**************************************************************************************************
  Function:         ControlSFP
  Description:      Control SFP.
  Calls:            none
  Called By:        dll
  Table Accessed:   none
  Table Updated:    none
  Input:            none
  Output:           error code
  Return:           none
  Test Case ID:     none
  Used Command:     g_szShmonOpticalLoopInital, g_szSFPWritePort0Cmd
**************************************************************************************************/

void __decldllexp PluginSFP(tTestData *testData, tTestError *testError)
{
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};

    int  error = 0;
    int  iPassFail = 1;

    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));

	// InetTelnetOpen(ShellCommand)
    g_hShellCommand = RfbbInetTelnetOpen (g_szDUTIP, g_iShellCommandPort, 0); 
	
    // Optical Loop Inital Run
	sprintf(kszShmonAgent,g_szShmonOpticalLoopInital);
	Shmon_RunCmdNoAck(kszShmonAgent,1);

    Delay(5.0);
    //--------------------------------------------------------------------
    sprintf( szLogMsg, "##### Plug In SFP #####" );
    ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
	
	// Add by Wendy on Sept.21th for manual Plugin SFP 
	// MessagePopup("Pay attention:", szLogMsg);
	 
  ErrChk(USBWriteDigToPort(g_szSFPWritePort0Cmd[0].ucTashName,g_szSFPWritePort0Cmd[0].ucDevport,g_szSFPWritePort0Cmd[0].uiWriteData));
  Delay(3.0);


    
Error:
    
#if(ON_OFF != SIMU_VERS) 
    
    if(iPassFail == 1)
    	{
        testData->result = PASS;
    	}
    else 
    	{
        testData->result = FAIL;
    	}

	// InetTelnetClose(ShellCommand) 
    InetTelnetClose(g_hShellCommand); 
	
    // FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    	{
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    	}
#else
    testData->result = PASS;
#endif

   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

   return;    
}



/**************************************************************************************************
  Function:         ReleaseSFP
  Description:      Release SFP.
  Calls:            none
  Called By:        dll
  Table Accessed:   none
  Table Updated:    none
  Input:            none
  Output:           error code
  Return:           none
  Test Case ID:     none
  Used Command:     g_szSFPWritePort0Cmd
**************************************************************************************************/
void __decldllexp ReleaseSFP(tTestData *testData, tTestError *testError)
{
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;

    int  error = 0;
    int  iPassFail = 1;

    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));

    //--------------------------------------------------------------------
    sprintf( szLogMsg, "##### Release SFP #####" );
    ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
    
	// Add by Wendy on Sept.21th for manual release SFP
	// MessagePopup("########Pls Plug out SFP#########",szLogMsg);
    
   ErrChk(USBWriteDigToPort(g_szSFPWritePort0Cmd[1].ucTashName,g_szSFPWritePort0Cmd[1].ucDevport,g_szSFPWritePort0Cmd[1].uiWriteData));
    Delay(1.0);

    
Error:
    
#if(ON_OFF != SIMU_VERS) 
    
    if(iPassFail == 1)
    	{
        testData->result = PASS;
    	}
    else 
    	{
        testData->result = FAIL;
    	}

    //InetTelnetClose(g_hShellCommand);
	InetTelnetClose(g_hShellCommand);  
	
    // FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    	{
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    	}
#else
    testData->result = PASS;
#endif

   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

   return;    
}

// ************************* 78 character ruler *****************************


